package demo2;



//鸭子类
public class Duck {
    // 鸭子有飞和叫两个表现方法
    private FlyBehavior flyBehavior;
    private SpeakBehavior speakBehavior;

    public Duck(FlyBehavior flyBehavior, SpeakBehavior speakBehavior) {
        this.flyBehavior = flyBehavior;
        this.speakBehavior = speakBehavior;
    }

    public void fly()
    {
        flyBehavior.fly();// 功能复用
    }
    public void quack()
    {
        speakBehavior.quack();// 功能复用
    }

    /*
     * 让用户可以更换飞行行为，增加灵活性
     * */
    public void setFlyBehavior(FlyBehavior flyBehavior)
    {
        this.flyBehavior=flyBehavior;
    }
    /*
     * 让用户可以更换发声行为，增加灵活性
     * */
    public void setSpeakBehavior(SpeakBehavior speakBehavior) {
        this.speakBehavior=speakBehavior;
    }
}
